﻿using System;
using System.Collections.Generic;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using System.Data;
using System.Data.SqlClient;


namespace SDLITS.Web.Driver
{
    public partial class DealCardDetail : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMemCardNo.Text))
            {
                Alert.Show("没有要修改的会员卡！");
                return;
            }
            if (Cutover())
            {
                SetPrintSession();
                PageContext.RegisterStartupScript("window.open('DealCardPrint.aspx', '打印交割单', 'height=600,top=10,width=880,toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no')");
                btnReset.Hidden = true;
            }
        }

        protected void txtMemCardNo_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMemCardNo.Text))
            {
                if (IsDriverCard())
                {
                    btnReset.Hidden = false;
                    GroupPanel1.Hidden = false;
                    GroupPanel2.Hidden = false;
                    GroupPanel3.Hidden = false;
                    GroupPanel4.Hidden = false;
                    GroupPanel6.Hidden = false;
                    GroupPanel7.Hidden = false;

                    V_MemCardInfoType card = SDDbSession.Default.From<V_MemCardInfoType>().Where(V_MemCardInfoType._.MemCardNo == txtMemCardNo.Text.Trim()).ToFirst();
                    if (card != null)
                    {

                        #region 1
                        txt_sfz.Text = card.IDNumber;
                        txt_name.Text = card.Name;
                        txt_addr.Text = card.IDAddress;
                        txt_sex.Text = card.Sex.ToString().ToLower() == "false" ? "女" : "男";
                        txt_birth.Text = card.Birthday.ToString();
                        txt_cell.Text = card.Cell;
                        txt_phone.Text = card.Phone;
                        txt_note1.Text = card.Custom1;
                        txt_note2.Text = card.Custom2;
                        hidmemid.Text = card.MemCardID.ToString();
                        hidregid.Text = card.RegistID;
                        img1.ImageUrl = string.Format("{0}{1}.bmp", @"/Img/IDShot/", card.IDNumber);
                        #endregion
                        #region 2
                        txt_start.Text = card.StartDate.ToString();
                        txt_end.Text = card.ExpireDate.ToString();
                        txt_cash.Text = card.LeftCash.ToString();
                        #endregion
                        #region 3
                        txt_drivername.Text = card.DLName;
                        rblsex.SelectedValue = string.IsNullOrEmpty(card.DLSex.ToString()) ? "1" : (Boolean.Parse(card.DLSex.ToString()) ? "1" : "2");
                        txt_cardno.Text = card.DLNumber;
                        txt_cardaddr.Text = card.DLAddress;
                        #endregion
                        #region 4
                        txt_carno.Text = card.CarNo;
                        txt_fdj.Text = card.CarEnginNo;
                        txt_bs.Text = card.CarIDNo;
                        txt_cz.Text = card.CarOwnerName;
                        txt_czdz.Text = card.CarOwnerAddr;
                        txt_gchm.Text = card.GCarNo;
                        txt_gcbs.Text = card.GCarIDNo;
                        #endregion
                        #region 6/7
                        SqlParameter[] sp = new SqlParameter[]{
                            new SqlParameter("@MessageID",Convert.ToInt32(Request.QueryString["id"])),
                            new SqlParameter("@SPAction","SelectByMessageIDForDeal")
                        };
                        DataSet Ds = SDDbSession.Default.FromProc("TableLEDMessage").AddParameter(sp).ToDataSet();
                        if (Ds != null && Ds.Tables.Count > 0 && Ds.Tables[0].Rows.Count > 0)
                        {
                            DataRow dr = Ds.Tables[0].Rows[0];
                            txtGoodName.Text = dr["GoodName"] is DBNull ? "" : dr["GoodName"].ToString();
                            txtAmount.Text = dr["Amount"] is DBNull ? "" : dr["Amount"].ToString();
                            //lblAmount.Text = dr["UnitType"].ToString();
                            txtCarInfo.Text = dr["CarInfo"] is DBNull ? "" : dr["CarInfo"].ToString();
                            txtTransPrice.Text = dr["TransPrice"] is DBNull ? "" : dr["TransPrice"].ToString();

                            lblStartPlace.Text = string.Format("{4} - {3} - {0}", dr["StartRegionName"],
                                                               dr["StartRegionNameEN"], dr["StartRegionShortName"],
                                                               dr["StartParentRegionName"], dr["StartTopRegionName"]);
                            lblEndPlace.Text = string.Format("{4} - {3} - {0}", dr["EndRegionName"],
                                                             dr["EndRegionNameEN"], dr["EndRegionShortName"],
                                                             dr["EndParentRegionName"], dr["EndTopRegionName"]);

                            txtCompanyName.Text = dr["CompanyName"] is DBNull ? "" : dr["CompanyName"].ToString();
                            lblRoom.Text = dr["Room"] is DBNull ? "" : dr["Room"].ToString();
                            txtContactPerson.Text = dr["ContactPerson"] is DBNull ? "" : dr["ContactPerson"].ToString();
                            txtCompanyPhone.Text = dr["Phone"] is DBNull ? "" : dr["Phone"].ToString();
                        }
                        #endregion
                    }
                    else
                    {
                        Alert.Show("没有该卡号信息");
                    }
                }
                else
                {
                    btnReset.Hidden = true;
                    GroupPanel1.Hidden = true;
                    GroupPanel2.Hidden = true;
                    GroupPanel3.Hidden = true;
                    GroupPanel4.Hidden = true;
                    GroupPanel6.Hidden = true;
                    GroupPanel7.Hidden = true;
                }
            }
        }
        private bool IsDriverCard()
        {
            int s= SDDbSession.Default.From<MemCard>().Where(MemCard._.MemTypeID == 1 && MemCard._.StatusID == 1 && MemCard._.MemCardNo == txtMemCardNo.Text.Trim()).Count();
            return s.Equals(1);
        }
        private bool Cutover()
        {
            int ds = 0;
            var mpPre = Session["ModelPrint"] as ModelPrint;
            if (mpPre != null)
            {
                SqlParameter[] sp = new SqlParameter[]{
                    new SqlParameter("@MessageID",Convert.ToInt32(Request.QueryString["ID"])),
                    new SqlParameter("@IsCutover",true),
                    new SqlParameter("@CutoverDriverIDNumber",txt_sfz.Text),
                    new SqlParameter("@CutoverCompanyIDNumber",mpPre.CompanyIDNumber),
                    new SqlParameter("@CutoverDate",DateTime.Now),
                    new SqlParameter("@SPAction","Cutover")
                };
                ds = SDDbSession.Default.FromProc("TableLEDMessage").AddParameter(sp).ExecuteNonQuery();
            }
            if (ds > 0)
            {
                return true;
            }
            else
            { 
                return false;
            }
        }
        private void SetPrintSession()
        {
            var mpPre = Session["ModelPrint"] as ModelPrint;
            if (mpPre != null)
            {
                var mp = new ModelPrint
                {
                    CompanyIDNumber = mpPre.CompanyIDNumber,
                    MemCardID = hidmemid.Text,
                    MemCardNo = txtMemCardNo.Text,
                    RegistID = hidregid.Text,
                    StartDate = txt_start.Text,
                    ExpireDate = txt_end.Text,
                    IDNumber = txt_sfz.Text,
                    Name = txt_name.Text,
                    Address = txt_addr.Text,
                    Sex = txt_sex.Text,
                    Birthday = txt_birth.Text,
                    Cell = txt_cell.Text,
                    Phone = txt_phone.Text,
                    LeftCash = txt_cash.Text,
                    DLName = txt_drivername.Text,
                    DLSex = rblsex.SelectedValue,
                    DLNumber = txt_cardno.Text,
                    DLAddress = txt_cardaddr.Text,
                    CarNo = txt_carno.Text,
                    CarEnginNo = txt_fdj.Text,
                    CarIDNo = txt_bs.Text,
                    CarOwnerName = txt_cz.Text,
                    CarOwnerAddr = txt_czdz.Text,
                    GCarNo = txt_gchm.Text,
                    GCarIDNo = txt_gcbs.Text,
                    GoodName = txtGoodName.Text,
                    AmountUnit =  txtAmount.Text,
                    CarInfo = txtCarInfo.Text,
                    TransPrice = txtTransPrice.Text,
                    StartPlace = lblStartPlace.Text,
                    EndPlace = lblEndPlace.Text,
                    CompanyName = txtCompanyName.Text,
                    Room = lblRoom.Text,
                    ContactPerson = txtContactPerson.Text,
                    CompanyPhone = txtCompanyPhone.Text,
                    PrintTime = DateTime.Now.ToString()
                };
                Session["ModelPrint"] = mp;
            }

        } 
    }
}